package Presentation;

import BusinessLogic.Command;
import BusinessLogic.Connection;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;

public class frmAddFriend extends javax.swing.JFrame {
    public String name="";
    
    public frmAddFriend() {
        initComponents();
        setIcon();
        this.setLocation(550, 100);
    }
    
    private void setIcon() {
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/Images/icon.png")));
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txt_Friendname = new javax.swing.JTextField();
        btn_OK = new javax.swing.JButton();
        btn_Cancel = new javax.swing.JButton();

        setTitle("Thêm bạn vào danh sách");
        setName("frm_ketban"); // NOI18N
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 204));
        jLabel1.setText("Nhập tên người dùng bạn muốn thêm");

        txt_Friendname.setName("txt_ten"); // NOI18N
        txt_Friendname.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txt_FriendnameKeyPressed(evt);
            }
        });

        btn_OK.setBackground(new java.awt.Color(204, 204, 255));
        btn_OK.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        btn_OK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/accept.png"))); // NOI18N
        btn_OK.setText("Kết bạn");
        btn_OK.setBorder(null);
        btn_OK.setName("btn_OK"); // NOI18N
        btn_OK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_OKActionPerformed(evt);
            }
        });

        btn_Cancel.setBackground(new java.awt.Color(204, 204, 255));
        btn_Cancel.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        btn_Cancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/delete_friend.png"))); // NOI18N
        btn_Cancel.setText("Hủy");
        btn_Cancel.setBorder(null);
        btn_Cancel.setName("btn_Cancel"); // NOI18N
        btn_Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_CancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(64, 64, 64)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(btn_OK, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btn_Cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(txt_Friendname, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(66, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txt_Friendname, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btn_Cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_OK, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    //Nút xác nhận được bấm
    private void btn_OKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_OKActionPerformed
        if(txt_Friendname.getText().equals("")){
            JOptionPane.showMessageDialog(this,"Phải nhập tên của người muốn kết bạn!","Thông báo",JOptionPane.ERROR_MESSAGE);
        }
        else{
            if(txt_Friendname.getText().equals(name)) 
                JOptionPane.showMessageDialog(this,"Không thể thêm chính mình!","Thông báo",JOptionPane.ERROR_MESSAGE);
            else{
                Connection.sendData(Command.ADD_FRIEND_REQUEST+name+((char)001)+txt_Friendname.getText());
            }
        }
    }//GEN-LAST:event_btn_OKActionPerformed

    private void btn_CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_CancelActionPerformed
        clear();
    }//GEN-LAST:event_btn_CancelActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    }//GEN-LAST:event_formWindowClosed
 
private void txt_FriendnameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_FriendnameKeyPressed
    if(evt.getKeyCode() == KeyEvent.VK_ENTER){
       btn_OKActionPerformed(null); 
    }
}//GEN-LAST:event_txt_FriendnameKeyPressed
    
    private void clear(){
        txt_Friendname.setText(null);
        txt_Friendname.requestFocus();
    }
    //Gán tên đang đăng nhập
    public void setUsername(String str){
        name = str;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Cancel;
    private javax.swing.JButton btn_OK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField txt_Friendname;
    // End of variables declaration//GEN-END:variables
}
